package com.tangcan.blog.Utils;


import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.auth0.jwt.JWT;

@EnableAutoConfiguration
public class TokenUtils {
    //过期时间是60min
    private static long EXPIRE_DATE = 3600*10000;
    //token秘钥

    private  static String TOKEN_SECRET = "ShiLeZhi.CLUB.WDNMD2021" ;

    public static String token (int id ,String username,String password,String authority){

        String token = "";
        try {
            //过期时间
            Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE);
            //秘钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头部信息
            Map<String,Object> header = new HashMap<>();
            header.put("typ","JWT");
            header.put("alg","HS256");
            //携带username，password,authority信息，生成签名
            token = JWT.create()
                    .withHeader(header)
                    .withClaim("id",id)
                    .withClaim("username",username)
                    .withClaim("password",password)
                    .withClaim("authority",authority)
                    .withExpiresAt(date)
                    .sign(algorithm);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        System.out.println(token);
        return token;
    }

    public static boolean verify(String token){
        /**
         * @desc   验证token，通过返回true
         * @params [token]需要校验的串
         **/
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
//            System.out.println(jwt.getClaims());
            return true;
        }catch (Exception e){
//            e.printStackTrace();
            return  false;
        }
    }

    public static Map getTokenInfo(String token)
    {
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            System.out.println(jwt.getClaims());
            return jwt.getClaims();
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
    }
//    public void main(String[] args) {
//        String username ="zhangsan";
//        String password = "123";
//        String token = token(username,password);
//        System.out.println(token);
//        boolean b = verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd22yZCI6IjEyMyIsImV4cCI6MTU3ODE5NzQxMywidXNlcm5hbWUiOiJ6aGFuZ3NhbiJ9.IyTZT0tISQQZhGhsNuaqHGV8LD7idjUYjn3MGbulmJg");
//        System.out.println(b);
//    }

}
